package com.tinklabs.demo.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.drakeet.multitype.MultiTypeAdapter;

import com.tinklabs.demo.R;
import com.tinklabs.demo.adapter.decoration.LinearSpaceItemDecoration;
import com.tinklabs.demo.adapter.multitype.CityGuideNormalViewBinder;
import com.tinklabs.demo.adapter.multitype.CityGuideSpecialViewBinder;
import com.tinklabs.demo.bean.CityGuideResult;
import com.tinklabs.demo.listener.OnVerticalScrollListener;
import com.tinklabs.demo.mvp.callback.CityGuideCallBack;
import com.tinklabs.demo.mvp.presenter.CityGuidePresenter;
import com.tinklabs.demo.utils.CollectionUtils;
import com.tinklabs.demo.utils.ToastUtils;

/**
 * Created by Jennifer on 2018/4/14.
 */

public class CityGuideFragment extends PresenterFragment<CityGuidePresenter, CityGuideCallBack>
        implements CityGuideCallBack, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.fragment_city_guide_swipeRefreshLayout)
    protected SwipeRefreshLayout swipeRefreshLayout;

    @BindView(R.id.fragment_city_guide_recyclerView)
    protected RecyclerView recyclerView;

    private List<CityGuideResult.Item> dataList;
    private MultiTypeAdapter adapter;
    private boolean isRefresh;


    @Override
    protected Fragment getFragment() {
        return this;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_city_guide;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        dataList = new ArrayList<>();

        intiSwipeRefreshLayout();
        initRecyclerView();
    }

    @Override
    protected void initData() {
        super.initData();
        //获取数据
        getPresenter().getData(getActivity());
    }

    private void intiSwipeRefreshLayout() {
        swipeRefreshLayout.setColorSchemeColors(Color.parseColor("#303F9F"));
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setRefreshing(true);
    }

    private void initRecyclerView() {
        recyclerView.addItemDecoration(new LinearSpaceItemDecoration(getActivity(), 15, 0));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        // 组装多样式Item数据
        adapter = new MultiTypeAdapter(dataList);
        adapter.register(CityGuideResult.NormalItem.class, new CityGuideNormalViewBinder());
        adapter.register(CityGuideResult.Item.class, new CityGuideSpecialViewBinder());
        recyclerView.setAdapter(adapter);

        initRecyclerViewListener();
    }

    private void initRecyclerViewListener() {
        recyclerView.addOnScrollListener(new OnVerticalScrollListener() {

            @Override
            public void onScrolledToBottom() {
                // load more data
                getPresenter().getData(getActivity());
            }
        });

    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        //模拟耗时操作
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                isRefresh = true;
                getPresenter().getData(getActivity());
            }
        }, 500);
    }

    @Override
    protected CityGuidePresenter createPresenter() {
        return new CityGuidePresenter();
    }

    @Override
    protected CityGuideCallBack createView() {
        return this;
    }


    /**
     * @param list 请求数据成功
     */
    @Override
    public void onSuccessResult(List<CityGuideResult.Item> list) {
        swipeRefreshLayout.setRefreshing(false);
        if (isRefresh) {
            dataList.clear();
            isRefresh = false;
        }

        if (!CollectionUtils.isEmpty(list)) {
            dataList.addAll(list);
            adapter.setItems(dataList);

            recyclerView.post(new Runnable() {
                public void run() {
                    adapter.notifyDataSetChanged();

                }
            });
        }
    }

    /**
     * @param error 请求数据失败
     */
    @Override
    public void onErrorResult(String error) {
        swipeRefreshLayout.setRefreshing(false);
        ToastUtils.showToast(getActivity(), error);
    }

}
